<?php
namespace application\cms\controller\admin;


class Page{

    //列表
    function index(){   
        $sql="select * from ".tablename( 'rj_cms_page' ) ;
        if(isset($_REQUEST['keyword']) && $_REQUEST['keyword']!='' ){
            $keyword = trim($_REQUEST['keyword']);
            $sql .= " where title like '%$keyword%' ";
        }        
        $sql.= " order by list_order";

        //$page=new \library\deep\Page(20);
        $page= \library\deep\Page::new_instance();

        $sql=$page->getPageSQL( $sql , 20 );

        $rs=$GLOBALS['db']->query($sql);
        
        require view('admin/page/page.tpl.php');
    } 

    //添加页面
    function page_add(){
        require view('admin/page/page_add.tpl.php');
    }

    //修改页面
    function page_edit(){
        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="select * from ".tablename('rj_cms_page')." where id=".$id;
            $ret=$GLOBALS['db']->query($sql)->fetch();
        }else{
            message('未找到该链接,请刷新页面',url([ _MODULE_NAME_ ,'admin/page','index']));
        }
        require view('admin/page/page_edit.tpl.php');
    }

    //保存
    function page_save(){
        if(!isset($_POST['title']) && $_POST['title']==''){
            message('标题不能为空',url([ _MODULE_NAME_ ,'admin/page','index']));
        }

        $data = [
            'title'=> $_POST['title'],
            'tip'=>$_POST['tip'],
            'content'=>$_POST['content'],
            'page_img'=> isset($_POST['page_img'])?$_POST['page_img']:'',
            'update_time'=> time(),
            'list_order'=> $_POST['list_order'],
            'tpl_name' => $_POST["tpl_name"] ,
            'status'=> $_POST['status'],
        ];

        if( isset($_POST['id']) && $_POST['id']!=''){
            //修改
            //条件
            $where['id']= intval($_POST['id']);

            //执行sql
            $ok = pdo_update('rj_cms_page',$data,$where);
            if($ok){
                message('修改成功',url([ _MODULE_NAME_ ,'admin/page','index']));
            }else{
                message('修改失败',url([ _MODULE_NAME_ ,'admin/page','index']));
            }
        }else{
            //添加
            $data['type']='单页';
            $data['create_time']=time();

            //执行sql
            $ok = pdo_insert('rj_cms_page',$data);
            if($ok){
                message('添加成功',url([ _MODULE_NAME_ ,'admin/page','index']));
            }else{
                message('添加失败',url([ _MODULE_NAME_ ,'admin/page','index']));
            }
        }
    }
    
    //删除
    function page_delete(){
        
        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="delete from ".tablename('rj_cms_page')." where id=".$id;
            $ok=$GLOBALS['db']->exec($sql);
            if($ok){
                message('删除成功',url([ _MODULE_NAME_ ,'admin/page','index']));
            }else{
                message('删除失败',url([ _MODULE_NAME_ ,'admin/page','index']));
            }
        }else{
            message('未找到该链接,请刷新页面',url([ _MODULE_NAME_ ,'admin/page','index']));
        }
    }

}
